package com.github.littleantfly.dictionary;


import com.github.littleantfly.dictionary.cache.DefaultDictCache;
import com.github.littleantfly.dictionary.cache.DictCache;
import com.github.littleantfly.dictionary.cache.loader.DictCacheLoader;
import com.github.littleantfly.dictionary.config.DictProperties;
import com.github.littleantfly.dictionary.init.DictCacheLoad;
import com.github.littleantfly.dictionary.service.DictService;
import com.github.littleantfly.dictionary.service.impl.DefaultDictServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author xiezhenglong
 */
@Configuration
@Import({JacksonDictAutoConfiguration.class, FastjsonDictAutoConfiguration.class})
public class DictionaryAutoConfiguration {
    private final static Logger logger = LoggerFactory.getLogger(DictionaryAutoConfiguration.class);

    @Bean
    @ConfigurationProperties(prefix = "dict")
    public DictProperties dictProperties() {
        return new DictProperties();
    }


    @Bean
    @ConditionalOnBean(DictCacheLoader.class)
    public DictCacheLoad dictCacheLoad(DictCacheLoader DictCacheLoader) {
        return new DictCacheLoad(DictCacheLoader);
    }

    @Bean
    @ConditionalOnMissingBean(DictCache.class)
    public DictCache dictCache() {
        return new DefaultDictCache();
    }

    @Bean("dictService")
    @ConditionalOnMissingBean(DictService.class)
    public DictService dictService(DictCache dictCache) {
        if (logger.isDebugEnabled()) {
            logger.debug("init default dictService");
        }
        return new DefaultDictServiceImpl(dictCache);
    }


}
